﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 渐变
{
    public partial class NoticeForm : Form
    {


        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x84:
                    base.WndProc(ref m);
                    if ((int)m.Result == 0x1)
                        m.Result = (IntPtr)0x2;
                    return;
            }

            base.WndProc(ref m);
        }

        /*[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
    (
        int nLeftRect, // x-coordinate of upper-left corner
        int nTopRect, // y-coordinate of upper-left corner
        int nRightRect, // x-coordinate of lower-right corner
        int nBottomRect, // y-coordinate of lower-right corner
        int nWidthEllipse, // height of ellipse
        int nHeightEllipse // width of ellipse
     );

        [DllImport("dwmapi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);

        [DllImport("dwmapi.dll")]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

        [DllImport("dwmapi.dll")]
        public static extern int DwmIsCompositionEnabled(ref int pfEnabled);

        private bool m_aeroEnabled;                     // variables for box shadow
        private const int CS_DROPSHADOW = 0x00020000;
        private const int WM_NCPAINT = 0x0085;
        private const int WM_ACTIVATEAPP = 0x001C;

        public struct MARGINS                           // struct for box shadow
        {
            public int leftWidth;
            public int rightWidth;
            public int topHeight;
            public int bottomHeight;
        }

        private const int WM_NCHITTEST = 0x84;          // variables for dragging the form
        private const int HTCLIENT = 0x1;
        private const int HTCAPTION = 0x2;

        protected override CreateParams CreateParams
        {
            get
            {
                m_aeroEnabled = CheckAeroEnabled();

                CreateParams cp = base.CreateParams;
                if (!m_aeroEnabled)
                    cp.ClassStyle |= CS_DROPSHADOW;

                return cp;
            }
        }

        private bool CheckAeroEnabled()
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                int enabled = 0;
                DwmIsCompositionEnabled(ref enabled);
                return (enabled == 1) ? true : false;
            }
            return false;
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_NCPAINT:                        // box shadow
                    if (m_aeroEnabled)
                    {
                        var v = 2;
                        DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
                        MARGINS margins = new MARGINS()
                        {
                            bottomHeight = 1,
                            leftWidth = 1,
                            rightWidth = 1,
                            topHeight = 1
                        };
                        DwmExtendFrameIntoClientArea(this.Handle, ref margins);

                    }
                    break;
                default:
                    break;
            }
            base.WndProc(ref m);

            if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT)     // drag the form
                m.Result = (IntPtr)HTCAPTION;

        }*/
        public NoticeForm()
        {
            InitializeComponent();
          /*  m_aeroEnabled = false;
            this.FormBorderStyle = FormBorderStyle.None;*/
        }
        public string id { get; set; }
        public string title { get; set; }
        public string content { get; set; }
        /// <summary>
        /// 设置窗体的Region
        /// </summary>
        public void SetWindowRegion()
        {
            GraphicsPath FormPath;
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 50);
            this.Region = new Region(FormPath);

        }
        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }

        /*protected override void OnResize(EventArgs e)
        {
            this.Region = null;
            SetWindowR();

        }

        private void SetWindowR()
        {
            System.Drawing.Drawing2D.GraphicsPath gPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 5, this.Width, this.Height - 5);
            gPath = GetRoundedRP(rect, 30); //后面的30是圆的角度，数值越大圆角度越大
            this.Region = new Region(gPath);
        }

        private System.Drawing.Drawing2D.GraphicsPath GetRoundedRP(Rectangle rect, int a)
        {
            int diameter = a;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
            gp.AddArc(arcRect, 180, 90);
            arcRect.X = rect.Right - diameter;
            gp.AddArc(arcRect, 270, 90);
            arcRect.Y = rect.Bottom - diameter;
            gp.AddArc(arcRect, 0, 90);
            arcRect.X = rect.Left;
            gp.AddArc(arcRect, 90, 90);
            gp.CloseFigure();
            return gp;
        }*/

        private void NoticeForm_Load(object sender, EventArgs e)
        {
            SetWindowRegion();
            txtId.Hide();
            txtId.Text = this.id;
            if(txtId.Text != "")
            {
                uiLabel1.Text = "修改公告";
                bunifuFlatButton1.Text = "修改";
                txtTitle.Text = this.title;
                uiRichTextBox1.Text = this.content;
                
            }
        }

        public void clear()
        {
            txtTitle.Text = "";
            uiRichTextBox1.Text = "";
        }
        public string opername { get; set; }
        
        private void bunifuFlatButton1_Click(object sender, EventArgs e)
        {
            if (bunifuFlatButton1.Text == "修改")
            {
                //修改
                string sql = "update notice set title=@title,contentText=@contentText,createtime=@createtime,opername=@opername where NId =@NId";
                SqlParameter[] ps =
              {
                new SqlParameter("@title",txtTitle.Text),
                new SqlParameter("@contentText",uiRichTextBox1.Text),
                new SqlParameter("@createtime",DateTime.Now),
                new SqlParameter("@opername",this.opername),
                new SqlParameter("@NId",this.id),
            };
                
                int res = DBHelper.ExecuteNonQuery(sql, ps);
                if (res > 0)
                {
                    this.ShowSuccessTip("修改成功");
                   
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.ShowErrorTip("修改失败");
                }

            }
            else //添加
            {
                string sql = "insert into notice(title,contentText,createtime,opername) values(@title,@content,@createtime,@opername)";
                SqlParameter[] ps =
               {
                new SqlParameter("@title",txtTitle.Text),
                new SqlParameter("@content",uiRichTextBox1.Text),
                new SqlParameter("@createtime",DateTime.Now),
                new SqlParameter("@opername",this.opername)
            };
             
                int res = DBHelper.ExecuteNonQuery(sql, ps);
                if (res > 0)
                {
                    this.ShowSuccessTip("添加成功");

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.ShowErrorTip("添加失败");
                }
            }
            
           
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
